package com.quanyan.stadium.utils.demo.SpringDemo.aware;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * Created by xiaoxiao on 17/1/18.
 */
@Service
public class AwareService implements BeanNameAware,ResourceLoaderAware{

    private String name;
    private ResourceLoader resourceLoader;

    @Override
    public void setBeanName(String name) {
        this.name = name;
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    public void output(){
        System.out.println("bean的名称是:"+name);
        Resource resource = resourceLoader.getResource("/workspace/quanyan_space/stadium/src/main/java/com/quanyan/stadium/utils/demo/SpringDemo/aware/test.txt");
        try {
            System.out.println("输出的加载文件的内容是"+ IOUtils.toString(resource.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
